<%@page import="transfer.Group"%>
<%@page import="content.GroupContent"%>
<%@page import="java.util.ArrayList"%>
<%@page import="transfer.UserRole"%>
<%@page import="content.UserContent"%>
<%@page import="entity.UsersLocal"%>
<%@page import="transfer.User"%>
<%@page import="entity.GroupsLocalHome"%>
<%@page import="entity.UsersLocalHome"%>
<%@page import="javax.naming.InitialContext"%>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry&language=en"></script>
<script type="text/javascript" src="../js/map.js"></script>
<script type="text/javascript" src="../js/helper.js"></script>

<%
    InitialContext context = new InitialContext();
    ArrayList roles = UserRole.getAllRoles();
    ArrayList groups = GroupContent.getAllGroups();
    User user = null;
    if (request.getParameter("id") != null) {
        user = UserContent.getUser(new Integer(request.getParameter("id")));
    }
    if (user == null && request.getParameter("id") != null) {

%>
<script>
    window.location.replace('admin.jsp?content=user&operation=create');
</script>
<%    }
    if (user == null) {
%>
<script>
    action = 'create';
</script>
<%} else {
%>
<script>
    action = 'edit';
</script>
<%    }
%>
<%
    if (user == null) {
%>
<div class="block-header">Create user</div>
<%} else {
%>
<div class="block-header" id="user_id" value="<%= request.getParameter("id")%>">Edit user</div>
<%
    }
%>
<div style="float: left; width: 485px;">
    <div class="box">
        <div class="table-view" style="padding: 10px 20px 10px 0px;">
            <div>Login:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getLogin()%>" type="text" required="" id="login" placeholder="Enter 2-40 symbols" maxlength="40" />
            <span class="error" id="loginTip"></span><br/>

            <div>First name:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getFirstName()%>" type="text" required="" id="first-name"  maxlength="40" />
            <span class="error" id="first-nameTip"></span><br/>

            <div>Last name:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getLastName()%>" type="text" required="" id="last-name"  maxlength="40"/>
            <span class="error" id="last-nameTip"></span><br/>

            <div>E-mail:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getEmail()%>" type="text" required="" id="email"   maxlength="60"/>
            <span class="error" id="emailTip"></span><br/>

            <div>Password:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getPassword()%>" type="password" required="" id="password" placeholder="Enter 6-40 symbols" maxlength="40"/>
            <span class="error" id="passwordTip"></span><br/>

            <div>Confirm password:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getPassword()%>" type="password" required="" id="confpassword" maxlength="40"/>
            <span class="error" id="confpasswordTip"></span><br/>

            <div>Country:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getAddress().getCountry()%>" type="text" required="" id="country"   maxlength="40"/>
            <span class="error" id="countryTip"></span><br/>

            <div>City:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getAddress().getCity()%>" type="text" required="" id="city"  maxlength="50"/>
            <span class="error" id="cityTip"></span><br/>

            <div>Street:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getAddress().getStreet()%>" type="text" required="" id="street"   maxlength="60"/>
            <span class="error" id="streetTip"></span><br/>

            <div>Building number:<span class="text-red">*</span></div> 
            <input value="<%= user == null ? "" : user.getAddress().getBuilding()%>" type="text" required="" id="home"  maxlength="40"/>
            <span class="error" id="homeTip"></span><br/>

            <div>Apartment:</div> 
            <input value="<%= user == null ? "" : (user.getAddress().getFlat() == null ? "" : user.getAddress().getFlat())%>" type="text" required="" id="appartment"  maxlength="15"/>
            <span class="error" id="appartmentTip"></span><br/>

            <div>Roles:</div>
            <span style="display: inline-table;">
                <%
                    for (int i = 0; i < roles.size(); i++) {
                        UserRole ur = (UserRole) roles.get(i);
                        if (ur.getIdRole() != UserRole.ID_CUSTOMER) {
                %>
                <input name="role" <%= (user != null && user.getRoles().contains(ur)) ? "checked" : ""%> id="<%= ur.getIdRole()%>" type="checkbox" text="<%= ur.getNameRole()%>"><%= ur.getNameRole()%><br/>
                <%
                } else {
                %>
                <input disabled checked name="role" id="<%= ur.getIdRole()%>" type="checkbox" text="<%= ur.getNameRole()%>"><%= ur.getNameRole()%><br/>
                <%
                        }
                    }
                %>
            </span>

            <div>Groups:</div>
            <!--<span style="display: inline-table;">-->
            <select  multiple size="10" class="group-list" id="group_selection">

                <%
                    for (int i = 0; i < groups.size(); i++) {
                        Group g = (Group) groups.get(i);
                %>
                <option name="group" id="<%= g.getIdGroup()%>" <%= (g.getUsers().contains(user)) ? "selected" : ""%> ><%= g.getNameGroup()%></option>
                <%
                    }
                %>
            </select>
            <!--</span>-->
        </div>

        <div class="hidden">
            <input type="text" required="" id="latitude"/><br/>
            <input type="text" required="" id="longitude"/><br/>                 
        </div>
    </div>

</div>

<div style="float: right; width: 495px;">
    <div class="sidebar" id="googlemap" style="width: 495px; height: 300px;"></div>
    <div  id="location" style="float: bottom;" name="radios" ></div>
</div>